// 常用js工具，依赖jquery



$(function() {
	// 关闭加载动画
	closeLoading();
});


/*
 关闭通用加载遮罩,用于打开页面时有加载动画，需要html中相应div配合使用
<div id="_loading" style="position:absolute;z-index:1000;top:0px;left:0px;width:100%;height:100%;background:#DDDDDB;text-align :center;padding-top:20%;">
  <h1>
  <font color="#15428B" size="5">加载中....</font>
  </h1>
</div>
*/

function closeLoading() {
	$('#_loading').fadeOut('100', function() {
		$(this).remove();
	});
}


/*
加载遮罩,无需HTML配合   MY_LOCK.lock();开启 MY_LOCK.unlock() 关闭

*/
MY_LOCK = {
	onkeydowntempfunc: "",
	lock: function() {
		onkeydowntempfunc = document.onkeydown;
		document.onkeydown = function() {
			return false;
		};
		var d = document.getElementById("_screen_lock_");
		if (d) {
			return;
		}
		var a = document.createElement("div");
		a.id = "_screen_lock_";
		a.innerHTML =
			'<div style="position: absolute;top: 50%;left: 50%;font-weight: bold;width:45px;">加载中<marquee direction="right">>>></marquee></div>';
		a.style.backgroundColor = "lightGray";
		a.style.position = "absolute";
		a.style.width = "100%";
		a.style.height = "100%";
		a.style.zIndex = 999;
		a.style.opacity = 0.7;
		a.style.filter = "alpha(opacity=70)";
		a.style.top = 0;
		a.style.left = 0;
		document.body.appendChild(a);
	},
	unlock: function() {
		document.onkeydown = onkeydowntempfunc;
		var d = document.getElementById("_screen_lock_");
		if (d) {
			d.parentNode.removeChild(d);
		}
	}
}


/*
毫秒转换为时间字符，格式 2020-01-01 01:11:20
*/
function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}

//检查密码是弱密码
function isSimplePassword(pwd) {
	//数字顺序、字母顺序、键盘顺序
	var srt =
		"01234567890_09876543210_abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ_zyxwvutsrqponmlkjihgfedcba_ZYXWVUTSRQPONMLKJIHGFEDCBA_asdfghjkl;'\_ASDFGHJKL;'\_lkjhgfdsa_LKJHGFDSA_qwertyuiop[]_QWERTYUIOP[]_poiuytrewq_POIUYTREWQ_zxcvbnm,./_ZXCVBNM,./_mnbvcxz_MNBVCXZ";
	var simplePassword =
		'123123,123321,abcdef,abcabc,abc123,a1b2c3,aaa111,123qwe,qwerty,qweasd,admin,password,p@ssword,passwd,iloveyou,5201314,asdfghjkl,monkey,letmein,trustno1,dragon,baseball,master,sunshine,ashley,bailey,passw0rd,shadow,superman,qazwsx,michael,football';
	//小于6位、相同数字字母,AABB型,AABBCC型 ,包含在以上srt字符串中,ABC123型,包含在以上simplePassword字符串中
	var result = pwd.length < 6 || /^([0-9a-zA-Z])\1{5,19}$/.test(pwd) || /^([0-9a-zA-Z])\1{1,18}([0-9a-zA-Z])\2{1,16}$/.test(
			pwd) || /^([0-9a-zA-Z])\1{1,18}([0-9a-zA-Z])\2{1,16}([0-9a-zA-Z])\3{1,14}$/.test(pwd) ||
		srt.indexOf(pwd) >= 0 || (srt.indexOf(pwd.substring(0, 3)) >= 0 && srt.indexOf(pwd.substring(3, 6)) >= 0) ||
		simplePassword.indexOf(pwd) >= 0;
	return result;
}

// 扩展 js 数组删除功能
if (!Array.prototype.remove) {
	Array.prototype.remove = function(val) {
		var index = -1;
		for (var i = 0; i < this.length; i++) {
			if (this[i] == val) {
				index = i;
				break;
			};
		}
		if (index > -1) {
			this.splice(index, 1);
		}
	};
}


// 处理ajax的jsonResult返回值,主要用于返回json格式{code:0/1}
function dealJsonR(resp,succ,fail){
	if(resp.code == 1){
		if(succ){
			succ(resp.data);
		}else{
			alert("操作成功");
		}
	}else{
		if(fail){
			fail(resp.msg);
		}else{
			alert(resp.msg);
		}
	}
}

/* 页面长度限制，用于html页面中显示文本，如果过长，显示时只显示给定长度内容加...全部内容信息将放入到label标签的title属性 */
function lentip(str, ln) {
	if (str && str.length > ln) {
		return "<label title='" + str + "' />" + str.substring(0, ln) + "...";
	}
	return str;
}

// 原生打开新的全屏窗口
function openFullWin(url) {
	var iWidth = 800; //弹出窗口的宽度;
	var iHeight = 600; //弹出窗口的高度;
	var new_window = window.open(url, '',
		'toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no,z-look=yes,alwaysRaised=yes,fullscreen=yes'
	);
	new_window.resizeTo(screen.availWidth, screen.availHeight);
}

// file元素选择图片后预览图片
function PreviewImage(fileObj, imgPreviewId, divPreviewId) {
	var allowExtention = ".jpg,.bmp,.gif,.png";// 允许上传文件的后缀名document.getElementById("hfAllowPicSuffix").value;
	var extention = fileObj.value.substring(fileObj.value.lastIndexOf(".") + 1)
			.toLowerCase();
	var browserVersion = window.navigator.userAgent.toUpperCase();
	if (allowExtention.indexOf(extention) > -1) {
		if (fileObj.files) {// HTML5实现预览，兼容chrome、火狐7+等
			if (window.FileReader) {
				var reader = new FileReader();
				reader.onload = function(e) {
					document.getElementById(imgPreviewId).setAttribute("src",
							e.target.result);
				}
				reader.readAsDataURL(fileObj.files[0]);
			} else if (browserVersion.indexOf("SAFARI") > -1) {
				alert("不支持Safari6.0以下浏览器的图片预览!");
			}
		} else if (browserVersion.indexOf("MSIE") > -1) {
			if (browserVersion.indexOf("MSIE 6") > -1) {// ie6
				document.getElementById(imgPreviewId).setAttribute("src",
						fileObj.value);
			} else {// ie[7-9]
				fileObj.select();
				if (browserVersion.indexOf("MSIE 9") > -1)
					fileObj.blur();// 不加上document.selection.createRange().text在ie9会拒绝访问
				var newPreview = document.getElementById(divPreviewId + "New");
				if (newPreview == null) {
					newPreview = document.createElement("div");
					newPreview.setAttribute("id", divPreviewId + "New");
					newPreview.style.width = document
							.getElementById(imgPreviewId).width
							+ "px";
					newPreview.style.height = document
							.getElementById(imgPreviewId).height
							+ "px";
					newPreview.style.border = "solid 1px #d2e2e2";
				}
				newPreview.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='"
						+ document.selection.createRange().text + "')";
				var tempDivPreview = document.getElementById(divPreviewId);
				tempDivPreview.parentNode.insertBefore(newPreview,
						tempDivPreview);
				tempDivPreview.style.display = "none";
			}
		} else if (browserVersion.indexOf("FIREFOX") > -1) {// firefox
			var firefoxVersion = parseFloat(browserVersion.toLowerCase().match(
					/firefox\/([\d.]+)/)[1]);
			if (firefoxVersion < 7) {// firefox7以下版本
				document.getElementById(imgPreviewId).setAttribute("src",
						fileObj.files[0].getAsDataURL());
			} else {// firefox7.0+
				document.getElementById(imgPreviewId).setAttribute("src",
						window.URL.createObjectURL(fileObj.files[0]));
			}
		} else {
			document.getElementById(imgPreviewId).setAttribute("src",
					fileObj.value);
		}
	} else {
		alert("仅支持" + allowExtention + "为后缀名的文件!");
		fileObj.value = "";// 清空选中文件
		// if(browserVersion.indexOf("MSIE")>-1){
		// fileObj.select();
		// document.selection.clear();
		// }
		// fileObj.outerHTML=fileObj.outerHTML;
	}
}
